#include "fxcc/mono/Clazz.h"

using namespace mono;

std::shared_ptr<mono::Object> mono::Clazz::NewObject()
{
    auto obj = mono_object_new(mono_domain_get(), klass);
    return std::make_shared<mono::Object>(obj);
};

bool Clazz::Init()
{
    // ztclog::info("try get clazz %s %s", m_Desc.m_Namespace.c_str(), m_Desc.m_Name.c_str());
    klass = mono_class_from_name(m_Desc.image, m_Desc.m_Namespace.c_str(), m_Desc.m_Name.c_str());

    if (!klass)
    {
        ztclog::debug("cannot from class %s %s ", m_Desc.m_Namespace.c_str(), m_Desc.m_Name.c_str());
        return false;
    };

    this->m_IsEnum = mono_class_is_enum(klass);

    void* iter = nullptr;
    MonoMethod* method = nullptr;

    while ((method = mono_class_get_methods(klass, &iter)))
    {
        const char* method_name = mono_method_get_name(method);

        MonoMethodSignature* signature = mono_method_signature(method);
        if (signature)
        {
            int param_count = mono_signature_get_param_count(signature);

            mono::Method::Desc desc;
            desc.m_Name = method_name;
            desc.m_NumParam = param_count;

            auto inst = std::make_shared<mono::Method>(desc, method);
            m_Methods[desc] = inst;
        }
    }

    GetAllMethods();

    {
        MonoClassField* field;
        void* iter2 = NULL;

        while ((field = mono_class_get_fields(klass, &iter2)))
        {

            auto inst = std::make_shared<mono::Field>(field);
            inst->SetValue(0);

            if (m_IsEnum)
            {
                m_EnumValues.push_back(inst);
            }
            else
            {
                m_Fields.push_back(inst);
            }
        }
    }
    return true;
}


bool mono::Clazz::GetAllMethods()
{
    auto klassIndex = klass;

    while (klassIndex) 
    {
        void* iter = nullptr;
        MonoMethod* method;

        while ((method = mono_class_get_methods(klassIndex, &iter)))
        {
            const char* method_name = mono_method_get_name(method);
            MonoMethodSignature* signature = mono_method_signature(method);

            if (signature)
            {
                int param_count = mono_signature_get_param_count(signature);

                mono::Method::Desc desc;
                desc.m_Name = method_name;
                desc.m_NumParam = param_count;

                if (m_Methods.find(desc) == m_Methods.end())
                {
                    auto inst = std::make_shared<mono::Method>(desc, method);
                    m_Methods[desc] = inst;
                }
            }
        }

        klassIndex = mono_class_get_parent(klassIndex);
    }
    return klassIndex;
}

bool Clazz::LoadMethod(const Method::Desc &desc)
{
    auto inst = std::make_shared<Method>(desc);
    if (inst->m_Available)
    {
        m_Methods[desc] = inst;
    }
    return inst->m_Available;
}


std::shared_ptr<Method> Clazz::GetMethod(const std::string &name, int numParam)
{

    Method::Desc desc;
    desc.m_Name = name;
    desc.m_NumParam = numParam;
    desc.clazz = klass;

    return m_Methods[desc];
}

bool mono::Clazz::HasMethod(const Method::Desc& desc)
{
    auto it = m_Methods.find(desc);
    return it != m_Methods.end();

}

bool mono::Clazz::LoadMethod(const std::string& name, const int& numParam)
{
    Method::Desc desc;
    desc.m_Name = name;
    desc.m_NumParam = numParam;
    desc.clazz = klass;

    return LoadMethod(desc);
};